/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Database;

import java.sql.*; // JDBC stuff.

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;

/**
 *
 * @author smulle
 */
public class DBObject {

    public static final String SEPARATOR = ",";
    public static final String FROM = " FROM ";
    public static final String SELECT = "SELECT ";
    public static final String INSERT_INTO = "INSERT INTO ";
    public static final String ORACLE_013 = "ORACLE_013";
    public static final String ORACLE_033 = "ORACLE_033";
    public static final String ORACLE_053 = "ORACLE_053";
    public static final String MYSQL_013 = "MYSQL_013";
    //private String MySQLURL = "jdbc:mysql://db.student.chalmers.se:3306/dat135_013";
    private String OracleURL = "jdbc:oracle:thin:@//db.student.chalmers.se:1521/kingu.ita.chalmers.se";
    private static final String USERNAME1 = "dat135_013";
    private static final String PASSWORD1 = "dat135_013 ";
    private static final String USERNAME2 = "dat135_033";
    private static final String PASSWORD2 = "dat135_033 ";
    private static final String USERNAME3 = "dat135_053";
    private static final String PASSWORD3 = "dat135_053 ";
    private Connection oracle1 = null;
    private Connection oracle2 = null;
    private Connection oracle3 = null;
    private Hashtable<String, Connection> connections = new Hashtable<String, Connection>();
    private ArrayList<Connection> connectionsList = new ArrayList<Connection>();

    public DBObject() {

    }

    public DBObject(Hashtable<String, Connection> connections) {
        Enumeration<String> keyIterator = connections.keys();
        while (keyIterator.hasMoreElements()) {
            connectionsList.add(connections.get(keyIterator.nextElement()));
        }
        this.connections = connections;
    }

    public void connect() throws SQLException {

        Iterator<Connection> it = connectionsList.iterator();
        while (it.hasNext()) {
            it.next().close();
        }

        connections = new Hashtable<String, Connection>();
        connectionsList = new ArrayList<Connection>();



        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
        oracle1 = DriverManager.getConnection(OracleURL, USERNAME1, PASSWORD1);
        oracle1.setAutoCommit(false);
        connections.put(ORACLE_013, oracle1);
        connectionsList.add(oracle1);
        oracle2 = DriverManager.getConnection(OracleURL, USERNAME2, PASSWORD2);
        oracle2.setAutoCommit(false);
        connections.put(ORACLE_033, oracle2);
        connectionsList.add(oracle2);
        oracle3 = DriverManager.getConnection(OracleURL, USERNAME3, PASSWORD3);
        oracle3.setAutoCommit(false);
        connections.put(ORACLE_053, oracle3);
        connectionsList.add(oracle3);

        String driver = "com.mysql.jdbc.Driver";        //mysql database driver

        Connection con = null;

        try {
            Class.forName(driver).newInstance();
            String url = "jdbc:mysql://db.student.chalmers.se:3306/dat135_013";
            String username = "dat135_013";
            String password = "dat135_013";
            con = DriverManager.getConnection(url, username, password);

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        con.setAutoCommit(false);
        connections.put(MYSQL_013, con);
        connectionsList.add(con);



    }

    

    public void commit() throws SQLException {
        if (connectionsList != null) {
            Iterator it = connectionsList.iterator();
            while (it.hasNext()) {
                ((Connection) it.next()).commit();
            }
        }
    }

    public void rollBack() throws SQLException {
        if (connectionsList != null) {
            Iterator it = connectionsList.iterator();
            while (it.hasNext()) {
                ((Connection) it.next()).rollback();
            }
        }
    }

    public void setSavePoint() throws SQLException {
    
        if (connectionsList != null) {
            Iterator it = connectionsList.iterator();
            while (it.hasNext()) {
                ((Connection) it.next()).setSavepoint();
            }
        }
    }

    public void closeConnections() throws SQLException {

        if (connectionsList != null) {
            Iterator it = connectionsList.iterator();
            while (it.hasNext()) {
                ((Connection) it.next()).close();
            }
        }
    }

    public Hashtable<String, Connection> getConnections() {
        return connections;
    }

    public ArrayList<Connection> getConnectionsList() {
        return connectionsList;
    }

    public Connection getConnection(String ID) {
        return connections.get(ID);
    }
}
